<?php
declare (strict_types = 1);

namespace app\controller;

use app\model\AdminModel;
use think\Request;

class Admin extends Base
{
//    获取所有管理员信息
    public function getAllAdminInfo(){

        return AdminModel::getAllAdminInfo();

    }
//    根据管理员id删除管理员
    public function deleteAdminByID(Request $request){

        $admin_ids=$request->param('admin_id');

        $admin= new AdminModel();

        //判断id是数组还是一个数值
        if(is_array($admin_ids)){
            $condition = 'admin_id in('.implode(',',$admin_ids).')';
        }else{
            $condition = 'admin_id='.$admin_ids;
        }

        if($admin->where($condition)->delete()){
            return json(['code'=>1]);
        }
        return json(['code'=>0]);
    }

//     添加管理员
    public function addAdmin(Request $request){

        $json = array();
        $admin_name = $request->param('admin_name');
        $admin_account = $request->param('admin_account');
        $admin_password = $request->param('admin_password');
        if (AdminModel::where('admin_account',$admin_account)->find() == null) {

            if(AdminModel::where('admin_name',$admin_name)->find() == null)
            {
                AdminModel::create([
                    'admin_name' => $admin_name,
                    'admin_account' => $admin_account,
                    'admin_password' => md5($admin_password),

                ]);
                $json['result'] = 1;  //创建成功
            }
            else{
                $json['result'] = 0;  //账户名称重复
            }
        } else {
            $json['result'] = -1; // 登录账户已经创建
        }
        return json($json);

    }

//    根据管理员id修改管理员用户名、密码
    public function updateAdminInfo(Request $request){

        $json = array();
        $admin_id = $request->param('admin_id');

        $admin_name = $request->param('admin_name');
        $admin_password = $request->param('admin_password');

        $admin=AdminModel::find($admin_id);

        if($admin!=null){

            if($admin_password!="" && $admin_name!=""){

                $admin->admin_password = md5($admin_password);
                $admin->admin_name = $admin_name;
                if(AdminModel::where('admin_name',$admin_name)->where('admin_id','<>',$admin_id)->find() == null){

                    $admin->save();
                    $json['result'] = 1;   //修改成功

                }else{
                    $json['result'] = 0;  //账户名称重复
                }

            }else if($admin_password=="" && $admin_name!=""){

                $admin->admin_name = $admin_name;
                if(AdminModel::where('admin_name',$admin_name)->where('admin_id','<>',$admin_id)->find() == null){

                    $admin->save();
                    $json['result'] = 1;   //修改成功

                }else{
                    $json['result'] = 0;  //账户名称重复
                }

            }else if($admin_password!="" && $admin_name==""){

                $admin->admin_password = md5($admin_password);
                $admin->save();
                $json['result'] = 1;   //修改成功

            }

        }
        else{
            $json['result'] = -1;
        }


        return json($json);

    }

//   根据token获取当前登录管理员信息
    public function getNowAdminInfo(Request $request){
        $admin_id = AdminModel::getAdmindByToken($request->param('token'));
        return AdminModel::getInfoByUserid($admin_id);

    }






    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
